package MyFragment;

import static android.app.Activity.RESULT_OK;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.qb.devtest.MainActivity;
import com.qb.devtest.MyApplication;
import com.qb.devtest.R;

import java.io.IOException;
import java.io.InputStream;

import CustomView.CustomAudioPlayer;
import CustomView.CustomVideoPlayer;
import utils.MyUtil;

public class FragmentMedia extends Fragment {
    private static final int A_RE_CODE_IMAGE = 1;
    private static final int A_RE_CODE_AUDIO = 2;
    private static final int A_RE_CODE_VIDEO = 3;
    public TextView tvImage,tvAudio,tvVideo;
    Context ctx = MyApplication.getInstance();
    MainActivity activity= (MainActivity) getActivity();
    Boolean videoIsFull = false;
    CustomVideoPlayer cVideoPlayer;
    CustomAudioPlayer cAudioPlayer;
    ImageView ivImage;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_media, container, false);
        tvImage = view.findViewById(R.id.tv_image);
        tvAudio = view.findViewById(R.id.tv_audio);
        tvVideo = view.findViewById(R.id.tv_video);

        Button btnImage = view.findViewById(R.id.btn_image);
        btnImage.setOnClickListener(view1 -> {
            chooseMedia("image");
        });
        Button btnAudio = view.findViewById(R.id.btn_audio);
        btnAudio.setOnClickListener(view1 -> {
            chooseMedia("audio");
        });
        Button btnVideo = view.findViewById(R.id.btn_video);
        btnVideo.setOnClickListener(view1 -> {
            chooseMedia("video");
        });
        cVideoPlayer = view.findViewById(R.id.custom_video_player);
        cAudioPlayer = view.findViewById(R.id.custom_audio_player);
        ivImage = view.findViewById(R.id.iv_image);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 在这里初始化你的Fragment视图组件
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        cVideoPlayer.stop();
        cAudioPlayer.stop();
    }

    // 打开多媒体文件
    public void chooseMedia(String flag) {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
//        intent.addCategory(Intent.CATEGORY_OPENABLE);
        if (flag.equals("image")) {
            intent.setType("image/*");
            startActivityForResult(intent, A_RE_CODE_IMAGE);
        } else if (flag.equals("audio")) {
            intent.setType("audio/*");
            startActivityForResult(intent, A_RE_CODE_AUDIO);
        } else if (flag.equals("video")) {
            intent.setType("video/*");
            startActivityForResult(intent, A_RE_CODE_VIDEO);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyUtil.showLog("onActivityResult="+requestCode,"");
        if (resultCode == RESULT_OK) {
            if (requestCode==A_RE_CODE_IMAGE) {
                String r = playImage(data);
                if (r.equals("success")) {
                    tvImage.setVisibility(View.GONE);
                } else {
                    tvImage.setText(r);
                    tvImage.setVisibility(View.VISIBLE);
                }
            } else if (requestCode==A_RE_CODE_AUDIO) {
                String r =playAudio(data);
                if (r.equals("success")) {
                    tvAudio.setVisibility(View.GONE);
                } else {
                    tvAudio.setText(r);
                    tvAudio.setVisibility(View.VISIBLE);
                }
            } else if (requestCode==A_RE_CODE_VIDEO) {
                String r = playVideo(data);
                if (r.equals("success")) {
                    tvVideo.setVisibility(View.GONE);
                } else {
                    tvVideo.setText(r);
                    tvVideo.setVisibility(View.VISIBLE);
                }
            }
        }
    }
    public String playImage(Intent data) {
        if (data==null) {
            return "无效的图片";
        } else {
            Uri uri = data.getData();
            try (InputStream inputStream = ctx.getContentResolver().openInputStream(uri)) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                ivImage.setImageBitmap(bitmap);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ivImage.getLayoutParams();
                layoutParams.width = FrameLayout.LayoutParams.WRAP_CONTENT;
                layoutParams.height = FrameLayout.LayoutParams.WRAP_CONTENT;
                ivImage.setLayoutParams(layoutParams);
                ivImage.setPadding(0, 0, 0, 0);
                return "success";
            } catch (IOException e) {
                e.printStackTrace();
                return "图片加载失败,"+e.getMessage();
            }
        }
    }
    public String playAudio(Intent data) {
        if (data==null) {
            return "无效的音频文件";
        } else {
            Uri uri = data.getData();
            try {
                cAudioPlayer.loadAudio(uri);
                return "success";
            } catch (Exception e) {
                e.printStackTrace();
                return "音频播放失败,"+e.getMessage();
            }
        }
    }
    public String playVideo(Intent data) {
        if (data==null) {
            return "无效的视频文件";
        } else {
            Uri uri = data.getData();
            try {
                cVideoPlayer.loadVideo(uri);
                return "success";
            } catch (Exception e) {
                e.printStackTrace();
                return "视频播放失败,"+e.getMessage();
            }
        }
    }
}